package com.slm.report.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.slm.report.databinding.AdapterItemNameBinding;
import com.slm.report.db.StudentInfoBean;
import com.slm.report.ui.StudentInfoActivity;

import java.util.List;

/**
 * @ProjectName: My Application
 * @Package: com.slm.report.adapter
 * @ClassName: StudentNameAdapter
 * @Description: java类作用描述
 * @Author: 王丹丹
 * @CreateDate: 2020/5/28 14:51
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/5/28 14:51
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class StudentNameAdapter extends BaseAdapter {
    private Context mContext;
    private List<StudentInfoBean> infoBeanList;
    private OnDelDataCallBack onDelDataCallBack;

    public interface OnDelDataCallBack {
        void deleteData(StudentInfoBean infoBean);
    }

    public StudentNameAdapter(Context mContext, List<StudentInfoBean> infoBeanList, OnDelDataCallBack onDelDataCallBack) {
        this.mContext = mContext;
        this.infoBeanList = infoBeanList;
        this.onDelDataCallBack = onDelDataCallBack;
    }

    @Override
    public int getCount() {
        return infoBeanList == null ? 0 : infoBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return infoBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AdapterItemNameBinding adapterItemNameBinding = null;
        if (convertView == null) {
            adapterItemNameBinding = AdapterItemNameBinding.inflate(LayoutInflater.from(mContext));
            convertView = adapterItemNameBinding.getRoot();
            convertView.setTag(adapterItemNameBinding);
        } else {
            adapterItemNameBinding = (AdapterItemNameBinding) convertView.getTag();
        }
        final StudentInfoBean studentInfoBean = infoBeanList.get(position);
        adapterItemNameBinding.txtName.setText(studentInfoBean.getName());
        adapterItemNameBinding.nameKey.setText(studentInfoBean.getName().substring(0, 1));
        String fisrtSpell = studentInfoBean.getFisrtSpell().toUpperCase();
        if (position == 0) {
            adapterItemNameBinding.txtTag.setVisibility(View.VISIBLE);
            adapterItemNameBinding.txtTag.setText(fisrtSpell);
            adapterItemNameBinding.imgLine.setVisibility(View.VISIBLE);
        } else {
            String lastFisrtSpell = infoBeanList.get(position - 1).getFisrtSpell().toUpperCase();
            if (fisrtSpell.equals(lastFisrtSpell)) {
                adapterItemNameBinding.txtTag.setVisibility(View.GONE);
                adapterItemNameBinding.imgLine.setVisibility(View.GONE);
            } else {
                adapterItemNameBinding.txtTag.setVisibility(View.VISIBLE);
                adapterItemNameBinding.txtTag.setText(fisrtSpell);
                adapterItemNameBinding.imgLine.setVisibility(View.VISIBLE);
            }
        }
        adapterItemNameBinding.delTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onDelDataCallBack != null) {
                    onDelDataCallBack.deleteData(studentInfoBean);
                }
            }
        });
        adapterItemNameBinding.clickView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) mContext).startActivityForResult(StudentInfoActivity.getIntent(mContext, studentInfoBean), 5);
            }
        });
        return convertView;
    }
}
